package com.e;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UserDetail
 */
public class UserDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Object user2;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String sid=request.getParameter("sid");
		
		//ArrayList<User> list = new ArrayList<User>(); 
		// list.add(new User("1001","admin","男",18,"中国北京")); 
		 //list.add(new User("1002","zjl","男",18,"中国河南"));
		// list.add(new User("1003","zzz","男",18,"中国上海"));
		// list.add(new User("1004","lll","男",18,"中国河南"));
		// list.add(new User("1005","www","男",18,"中国河南"));
		
		ArrayList<User> alist = new ArrayList<User>(); 
		String sqlString="select sid,sname,sex,sno from student where sid='"+sid+"'";
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						alist.add(new User(rsSet.getInt("sid"),rsSet.getString("sname"),rsSet.getString("sex"),
								rsSet.getString("sno")));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
		User user2=null;
		for (User user : alist) {
				user2=user;
		}
		//for (User user : alist) {
		//	if (user.getSid().equals(sid)) {
		//		user2=user;
		//	}
	    //}
		request.setAttribute("user", user2);
		request.getRequestDispatcher("userdetail.jsp").forward(request, response); 
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
